/*
 *  $Id: SoulCage.cs 1058 2010-05-19 15:44:27Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Air.Spells {
    public class SoulCage: TargetEnchantmentCard<Creature> {
        public SoulCage(): base("001_a_s_soul_cage") {
            Level = CompleteLevel.ReadyForTest;
            Version = 0;
            Element = ElementType.Air;
            Value = 100;
            Target = CreatureTarget.Both;
            AddManaCost(ElementType.Air, 3);
        }

        protected override void DoAction(TargetArgs<Creature> args) {
            base.DoAction(args);
            args.Target.AddAffect(new TimeAffect(4, new Affects.Unbind()));
        }

        protected override EnchantmentAffect CreateEnchantment() {
            return new SoulCageAffect(true);
        }

        public class SoulCageAffect: EnchantmentAffect<SoulCage> {
            private bool _stealCard;

            public SoulCageAffect(bool stealCard) {
                _stealCard = stealCard;
                Add(new Affects.Berserk());
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.OnDeath += DoDeath;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnDeath -= DoDeath;
            }

            private void DoDeath(AbstractObject obj) {
                Creature cr = obj as Creature;
                if (cr != null && !cr.IsInstantDeath) {
                    foreach (ElementType element in ElementType.AllElements)
                        if (cr.Card.ManaCost[element] > 0)
                            cr.Owner.IncrementMana(element, cr.Card.ManaCost[element]);
                    if (_stealCard) {
                        cr.SetNoneCardReturn(true);
                        Caster.CardToHands(true, true, cr.Card);
                    }
                }
            }
        }
    }
}
